package com.lyf.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyf.comon.ResponseResult;
import com.lyf.constant.CommonConstants;
import com.lyf.entity.Admin;
import com.lyf.entity.Car;
import com.lyf.entity.SlideShow;
import com.lyf.exception.GlobalExceptionHandler;
import com.lyf.mapper.SlideShowMapper;
import com.lyf.qo.SlideShowQuery;
import com.lyf.service.SlideShowService;
import com.lyf.utils.WebUtils;
import com.lyf.vo.SlideShowVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import static com.lyf.constant.RedisConstants.*;

@Slf4j
@Service
public class SlideShowServiceImpl extends ServiceImpl<SlideShowMapper, SlideShow> implements SlideShowService {
    @Autowired
    private SlideShowMapper slideShowMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public SlideShow findSlidShowById(Long id) throws Exception {
        String key = SLIDE_SHOW_KEY + id;
        String SlideShowAll = redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(SlideShowAll)) {
            SlideShow slideShow = JSON.parseObject(SlideShowAll, SlideShow.class);
            log.info("走缓存");
            return slideShow;
        }
        //判断命中是否为空值 " "字符串
        if ("".equals(SlideShowAll)) {
            throw new Exception("这个轮播图不存在");
        }
        SlideShow slideShow = slideShowMapper.selectById(id);

        if (slideShow == null) {
            redisTemplate.opsForValue().set(key, "", 1, TimeUnit.MINUTES);
            throw new Exception("这个轮播图信息不存在");
        }
        redisTemplate.opsForValue().set(key, JSON.toJSONString(slideShow), CommonConstants.ONE, TimeUnit.HOURS);
        log.info("存入缓存");
        return slideShow;
    }

    @Override
    public IPage<SlideShow> listPage(SlideShowQuery query) throws Exception {
        //分页
        Page<SlideShow> page = new Page<>();
        page.setCurrent(query.getCurrentPage());
        page.setSize(query.getPageSize());
        //条件查询
        LambdaQueryWrapper<SlideShow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(query.getType() != null , SlideShow::getType , query.getType());
        //分页
        IPage<SlideShow> result = slideShowMapper.selectPage(page, lambdaQueryWrapper);

        return result;
    }

    @Override
    public List<SlideShowVO> findByType(Integer id) throws GlobalExceptionHandler {
        //条件查询
        LambdaQueryWrapper<SlideShow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SlideShow::getType , id);
        List<SlideShow> slideShowList = list(lambdaQueryWrapper);
        List<SlideShowVO> slideShowVOS = new ArrayList<>();
        for (SlideShow slideShow : slideShowList) {
            SlideShowVO slideShowVO = new SlideShowVO();
            slideShowVO.setSlideShowId(slideShow.getId());
            slideShowVO.setBannerId(slideShow.getBannerId());
            slideShowVO.setType(slideShow.getType());
            slideShowVO.setTitle(slideShow.getTitle());
            slideShowVO.setBannerUrl(slideShow.getBannerUrl());
            if(slideShow.getType()==1){
                slideShowVO.setHomeType(slideShow.getHomeType());
            }
            if(slideShow.getType()==1 && slideShow.getHomeType()==2){
                slideShowVO.setScenicSpotId(slideShow.getScenicSpotId());
            }
            if(slideShow.getType()==3){
                slideShowVO.setScenicSpotId(slideShow.getScenicSpotId());
            }
            slideShowVOS.add(slideShowVO);
        }
        return slideShowVOS;
    }

    @Override
    public ResponseResult saveOrUpdateSlideShow(SlideShow slideShow) throws GlobalExceptionHandler {
        Date date = new Date();
        Long userId = WebUtils.getUserId();
        if (slideShow == null) {
            return ResponseResult.fail();
        }
        Long id = slideShow.getId();
        if (id == null) {
            slideShow.setAddTime(date);
            slideShow.setAddUser(userId);
            return save(slideShow) ? ResponseResult.success() : ResponseResult.fail();
        } else {
            slideShow.setUpdateTime(date);
            slideShow.setUpdateUser(userId);
            //1.更新数据库
            //2.删除缓存
            boolean update = updateById(slideShow);
            if (!update) {
                return ResponseResult.fail();
            }
            redisTemplate.delete(SLIDE_SHOW_KEY + id);
            return ResponseResult.success(update);
        }
    }

    @Override
    public void deleteById(Long id) throws GlobalExceptionHandler {
        String key = SLIDE_SHOW_KEY + id;
        slideShowMapper.deleteById(id);
        redisTemplate.delete(key);
        redisTemplate.delete(SLIDE_SHOW_KEY);
    }

    @Override
    public boolean saveBatch(Collection<SlideShow> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<SlideShow> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<SlideShow> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(SlideShow entity) {
        return false;
    }

    @Override
    public SlideShow getOne(Wrapper<SlideShow> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<SlideShow> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<SlideShow> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

}
